package de.ifi.lmu.snitzelhunt.view;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.ifi.lmu.snitzelhunt.R;
import de.ifi.lmu.snitzelhunt.creator.Globals;
import de.ifi.lmu.snitzelhunt.util.DatabaseUtility;
import de.ifi.lmu.snitzelhunt.util.RouteServerUtility;
import de.ifi.lmu.snitzelhunt.util.basics.RouteInfo;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncDatabase_Route_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_RouteInfo_ReadforCreatorResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_RouteInfo_ReadforCreatorResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_ReadResponse;

public class Activity_MyRoutes extends ActionBarActivity implements AsyncServer_RouteInfo_ReadforCreatorResponse, AsyncServer_Route_ReadResponse, AsyncDatabase_Route_CreateResponse {

	private ArrayList<RouteInfo> myCreatedRoutes;
	private String myNumber;
	private RouteInfoListAdapter routeListAdapter;
	private ListView routeInfoList;
	private SharedPreferences settings;
	private SharedPreferences.Editor editor;
	private DatabaseUtility dbUtil;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_activity__my_routes);

		settings = getSharedPreferences(Globals.PREF_NAME, 0);
		editor = settings.edit();
		dbUtil = new DatabaseUtility();
		
		routeInfoList = (ListView) findViewById(R.id.myRoutesList);
		routeInfoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				RouteInfo selectedRI = (RouteInfo) routeInfoList.getItemAtPosition(arg2);
				getSelectedRouteFromServer(selectedRI.getGlobal_routeId());
			}
			
		});
		
		myCreatedRoutes = new ArrayList<RouteInfo>();
		myNumber = getIntent().getStringExtra("myNumber");
		routeListAdapter = new RouteInfoListAdapter(this,  myCreatedRoutes);
		routeInfoList.setAdapter(routeListAdapter);
		
		new RouteServerUtility().readRouteInfo_forCreator(myNumber, this);
	}

	
	private void getSelectedRouteFromServer(long globalID) {
		new RouteServerUtility().readRoute(globalID, this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity__my_routes, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void processServer_RouteInfo_ReadforCreator(
		Server_RouteInfo_ReadforCreatorResponse response) {
		myCreatedRoutes.clear();
		myCreatedRoutes.addAll(response.getRinfo().getInfo());
		routeListAdapter.notifyDataSetChanged();
		
		
	}

	

	private class RouteInfoListAdapter extends ArrayAdapter<RouteInfo> {
		
		private Context myContext;
		private ArrayList<RouteInfo> values;

		public RouteInfoListAdapter(Context context, ArrayList<RouteInfo> objects) {
			super(context, R.layout.created_routes_list_rowlayout, objects);
			// TODO Auto-generated constructor stub
			this.values = objects;
			this.myContext = context;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowView = inflater.inflate(R.layout.created_routes_list_rowlayout, parent,false);
			
			TextView textView = (TextView) rowView.findViewById(R.id.myRoutesRowLabel);
			Log.i("adapter", "textview:" + (textView == null)+ "values:"+(values.size())+"position"+position);
			
			textView.setText(values.get(position).getName());
			
			return rowView;
		}
		
	}



	@Override
	public void processServer_Route_Read(Server_Route_ReadResponse response) {
		dbUtil.createRoute(response.getRecvRoute(), getBaseContext(), this);
		Log.i("myRoutes", "received global id "+response.getRecvRoute().getGlobal_routeId());
	}


	@Override
	public void processDatabase_Route_Create(long localID) {
		editor.putLong(Globals.LAST_LOCAL_ID, localID);
		editor.putBoolean(Globals.ROUTE_IS_FROM_SERVER, true);
		editor.commit();
		
		Intent intent = new Intent(this, Activity_CreateHunt.class);
		intent.putExtra("loadPrevious", 1);
		startActivity(intent);
	}

}